import axios from 'axios'
import envFuncs from '@/config/base'
let env = envFuncs.getParams()
import { U, getRequestId } from '../../lib/backend'

let CRM_API_HOST = env.CRM_API_HOST

// 接口文档 http://rapapi.org/workspace/myWorkspace.do?projectId=18476
// 联系管理员才能查看
export default {
  getCounsellors ({state, rootState}, data) {
    const api = env.CRM_API_HOST
    return axios.get(api + '/sites/' + rootState.app.site_id + '/users/' + rootState.app.token + '/teams/' + data.team_id + '/counsellors')
  },

  // ----- 客户分配设置功能接口列表 开始--------
  // 查询客户分配规则的顾问列表
  getDistributeConfigUsers ({state}, data) {
    const api = env.CRM_API_HOST
    // const api = 'http://120.24.48.9:8889/http://rapapi.org/mockjsdata/18476'
    return axios.get(api + '/teams/' + data.team_id + '/distribute_config/' + data.distribute_type + '/users')
  },

  // 查询客户分配设置详情
  getDistributeConfig ({state}, teamId) {
    const api = env.CRM_API_HOST
    // const api = 'http://120.24.48.9:8889/http://rapapi.org/mockjsdata/18476'
    console.log(teamId)
    return axios.get(api + '/teams/' + teamId + '/distribute_config')
  },

  // 根据顾问id查询顾问分配比例
  getCounselorDistributeConfig (suid) {
    const api = env.CRM_API_HOST
    // const api = 'http://120.24.48.9:8889/http://rapapi.org/mockjsdata/18476'
    return axios.get(api + '/teams/' + suid + '/distribute_config')
  },

  // 设置客户分配的顾问和分配比例
  saveCounselorDistributeConfig ({state}, data) {
    const api = env.CRM_API_HOST
    // const api = 'http://120.24.48.9:8889/http://rapapi.org/mockjsdata/18476'
    const params = JSON.stringify({
      info: {
        distribute_order: data.distribute_order,
        distribute_type: data.distribute_type,
        max_list_count: data.max_list_count,
        suid: data.suid,
        team_dist_id: data.team_dist_id,
        id: data.id
      },
      trace: {
        requestId: getRequestId()
      }
    })
    return axios.post(api + '/teams/' + data.team_id + '/distribute_config/user', params)
  },

  // 设置客户分配规则
  // 第一个参数是team_id，团队id
  saveDistributeConfig ({state, rootState}, data) {
    const api = env.CRM_API_HOST
    // const api = 'http://120.24.48.9:8889/http://rapapi.org/mockjsdata/18476'
    const params = JSON.stringify({
      info: {
        distribute_type: data.distribute_type,
        is_allow_grab: data.is_allow_grab
      },
      trace: {
        requestId: getRequestId()
      }
    })
    return axios.post(api + '/teams/' + data.team_id + '/distribute_config?' + U({site_id: rootState.app.user.site_id }), params)
  },

  // ----- 客户分配设置功能接口列表 结束--------

  // ----- 客户释放设置功能 开始--------

  // 查询客户再分配限制详情
  // 包括统一规则和特殊规则列表，第一个参数是team_id，团队id
  getDistributeLimit ({state}, teamId) {
    const api = env.RELEASE_API_HOST
    // const api = 'http://120.24.48.9:8889/http://rapapi.org/mockjsdata/18476'
    return axios.get(api + '/teams/' + teamId + '/distribute_limit')
  },

  // 查询客户释放设置详情
  // 第一个参数是team_id，团队id
  getReleaseConfig ({state}, teamId) {
    const api = env.RELEASE_API_HOST
    // const api = 'http://120.24.48.9:8889/http://rapapi.org/mockjsdata/18476'
    return axios.get(api + '/teams/' + teamId + '/release_config')
  },

  // 根据顾问id查询顾问的再分配限制规则
  // 第一个参数是team_id，团队id，第二个参数是suid，企业营销用户id
  getCounselorReleaseConfig ({state}, data) {
    const api = env.RELEASE_API_HOST
    // const api = 'http://120.24.48.9:8889/http://rapapi.org/mockjsdata/18476'
    return axios.get(api + '/teams/' + data.team_id + '/users/' + data.suid + '/distribute_limit')
  },

  // 根据顾问id查询顾问的再分配限制规则
  // 第一个参数是team_id，团队id，第二个参数是suid，企业营销用户id
  saveCounselorDistributeLimit ({state}, data) {
    const api = env.RELEASE_API_HOST
    // const api = 'http://120.24.48.9:8889/http://rapapi.org/mockjsdata/18476'
    const params = JSON.stringify({
      info: {
        forbid_grab_day: data.forbid_grab_day
      },
      trace: {
        requestId: getRequestId()
      }
    })
    return axios.post(api + '/teams/' + data.team_id + '/users/' + data.suid + '/distribute_limit', params, {
      headers: {
        'Content-Type': 'application/json;charset=UTF-8'
      }})
  },
  deleteCounselorDistributeLimit ({state}, data) {
    const api = env.RELEASE_API_HOST
    // const api = 'http://120.24.48.9:8889/http://rapapi.org/mockjsdata/18476'
    return axios.delete(api + '/teams/' + data.team_id + '/users/' + data.suid + '/distribute_limit', {
      headers: {
        'Content-Type': 'application/json;charset=UTF-8'
      }})
  },
  // 设置客户再分配统一规则
  saveDistributeLimit ({state}, data) {
    const api = env.RELEASE_API_HOST
    // const api = 'http://120.24.48.9:8889/http://rapapi.org/mockjsdata/18476'
    const params = JSON.stringify({
      info: {
        forbid_grab_day: data.forbid_grab_day
      },
      trace: {
        requestId: getRequestId()
      }
    })
    return axios.post(api + '/teams/' + data.team_id + '/distribute_limit', params, {
      headers: {
        'Content-Type': 'application/json;charset=UTF-8'
      }})
  },

  // 设置客户释放规则
  // 第一个参数是team_id，团队id
  saveReleaseConfig ({state}, data) {
    const api = env.RELEASE_API_HOST
    // const api = 'http://120.24.48.9:8889/http://rapapi.org/mockjsdata/18476'
    const params = JSON.stringify({
      info: {
        agreement_day: data.agreement_day, // 签约无跟进释放天数
        agreement_limit_day: data.agreement_limit_day, // 签约无跟进限制抢天数
        confirm_day: data.confirm_day, // 到访无跟进释放天数
        confirm_limit_day: data.confirm_limit_day, // 到访无跟进限制抢天数
        deal_day: data.deal_day, // 成交无跟进释放天数
        deal_limit_day: data.deal_limit_day, // 成交无跟进限制抢天数
        init_day: data.init_day, // 初步接洽无跟进释放天数
        init_limit_day: data.init_limit_day, // 初步接洽无跟进限制抢天数
        is_use_no_deal: data.is_use_no_deal, // 是否启用无成交释放
        is_use_no_follow: data.is_use_no_follow, // 是否启用无跟进释放
        no_deal_day: data.no_deal_day, // 无成交订单释放天数
        no_deal_limit_day: data.no_deal_limit_day, // 无成交订单限制抢天数
        price_day: data.price_day, // 报价无跟进释放天数
        price_limit_day: data.price_limit_day, // 报价无跟进限制抢天数,
        arrive_day: data.arrive_day,
        arrive_limit_day: data.arrive_limit_day,
        custom_care_limit_day: data.custom_care_limit_day,
        custom_care_day: data.custom_care_day,
        initial_contract_day: data.initial_contract_day,
        initial_contract_limit_day: data.initial_contract_limit_day,
        new_custom_day: data.new_custom_day,
        new_custom_limit_day: data.new_custom_limit_day,
        renewal_contract_day: data.renewal_contract_day,
        renewal_contract_limit_day: data.renewal_contract_limit_day,
        reserve_day: data.reserve_day,
        reserve_limit_day: data.reserve_limit_day
      },
      trace: {
        requestId: getRequestId()
      }
    })
    return axios.post(api + '/teams/' + data.team_id + '/release_config', params, {
      headers: {
        'Content-Type': 'application/json;charset=UTF-8'
      }})
  },
  upgradeTeamMember ({ rootState }, info) {
    // let CRM_API_HOST = ' http://rap.taobao.org/mockjsdata/18476'
    let url = CRM_API_HOST + '/users/' + rootState.app.token + '/member/upgrade/team_manager'
    // let url = 'http://rap2api.taobao.org/app/mock/2717/GET//example/1514357201480'
    return axios.post(url, info)
  },
  downgradeTeamManager ({ rootState }, info) {
    // let CRM_API_HOST = ' http://rap.taobao.org/mockjsdata/18476'
    let url = CRM_API_HOST + '/users/' + rootState.app.token + '/team_manager/demotion/member'
    // let url = 'http://rap2api.taobao.org/app/mock/2717/GET//example/1514357201480'
    return axios.post(url, info)
  },
  // ----- 获取团队详情 开始--------
  // 查询获取团队的顾问列表
  getTeamDetail ({rootState, state}, data) {
    const api = env.CRM_API_HOST
    // const api = 'http://120.24.48.9:8889/http://rapapi.org/mockjsdata/18476'
    return axios.get(api + '/users/' + rootState.app.token + '/teams/' + data.team_id)
  }
  // ----- 客户释放设置功能 结束--------

}
